package com.easyandroid.test;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

import com.easyandroid.sqlite.BaseEASQLiteOpenHelper;
import com.easyandroid.test.tables.TestModelSchool;
import com.easyandroid.test.tables.TestModelUser;

public class DBHelper extends BaseEASQLiteOpenHelper {
    public static final String DB_NAME = "db5";
    public static final int DB_VERSION = 3;
    
    protected static DBHelper mInstance;
    
    public static void createInstance(Context context) {
        if (null == mInstance) {
            mInstance = new DBHelper(context);
        }
    }

    public static BaseEASQLiteOpenHelper getInstance() {
        return mInstance;
    }

    public DBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(new TestModelUser().createTableSql());
        db.execSQL(new TestModelSchool().createTableSql());
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL(new TestModelUser().dropTableSql());
        db.execSQL(new TestModelSchool().dropTableSql());
        onCreate(db);
    }

    public Cursor query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) {
        return getReadableDatabase().query(table, columns, selection, selectionArgs, groupBy, having, orderBy);
    }

    public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {
        return getWritableDatabase().update(table, values, whereClause, whereArgs);
    }

    public int delete(String table, String whereClause, String[] whereArgs) {
        return getWritableDatabase().delete(table, whereClause, whereArgs);
    }

    public long insert(String table, String nullColumnHack, ContentValues values) {
        return getWritableDatabase().insert(table, nullColumnHack, values);
    }

    public Cursor rawQuery(String sql, String[] selectionArgs) {
        return getWritableDatabase().rawQuery(sql, selectionArgs);
    }
}
